# -*- coding: utf-8 -*-

from question import Question
from answerWidgets import PercentWidget, AnswerLine


class Multiple_Answer(Question):
    def __init__(self):
        super(Multiple_Answer, self).__init__()

        self.questionName = self.tr("Multiple answer")
        self.setObjectName("Multiple_Answers")
        self.answers.setScheme((PercentWidget, AnswerLine))
        self.answers.setPrefix("~")
        self.defaultAnswers = 1
        
        # pokud je prekrocena procentualni hranice, je zaznamenana chyba a otazky nelze ulozit
        #self.percentError = False

        # regexp na test a vytahnuti procentualni hodnoty odpovedi
        #self.percentRegexp = re.compile("%(-?[0-9.]*)%(.*)")
        

#    def addAnswer(self, text="", percentValue=0, checked=False):
#
#        self.drawPercentWidget(checked, percentValue, self.rowCount, 0)
#        self.objectMap["answer%s_checkBox" % self.rowCount] = "answer%s_percentBox" % self.rowCount
#
#        answer = QtGui.QLineEdit(text)
#        answer.setObjectName("answer%s" % self.rowCount)
#
#        answerDeleteWidget = QtGui.QPushButton()
#        answerDeleteWidget.setIcon(QtGui.QIcon(":/icons/delete.png"))
#
#        self.ui.answersLayout.addWidget(answer, self.rowCount, 3)
#        self.ui.answersLayout.addWidget(answerDeleteWidget, self.rowCount, 4)
#
#        deleteWidgetIndex = self.ui.answersLayout.indexOf(answerDeleteWidget)
#        deleteWidgetRow = self.ui.answersLayout.getItemPosition(deleteWidgetIndex)[0]
#        self.rowList.append(deleteWidgetRow)
#
#        QtCore.QObject.connect(answerDeleteWidget, QtCore.SIGNAL("clicked()"), self.deleteRow)
#
#        self.rowCount += 1


    def validateQuestion(self):
        """ kontrola validity otazky """

        self.basicValidate()

        # kontrola na procentualni soucet
        if self.percentError == True:
            self.errors.append(self.tr("Percentual sum mustn't go over 100 %."))


#    def getData(self):
#        """ vraci otazku v GIFT formatu, pouziva se pri ulozeni a nahledu otazky """
#
#        # text otazky
#        questionText = self.getQuestion()
#        # nazev otazky
#        questionTitle = self.getQuestionTitle()
#
#        # tady sbiram zformatovane odpovedi v GIFT formatu
#        answers = str()
#
#        for row in self.rowList:
#            answerPercent = self.getPercent(row, 1)
#            answer = self.ui.answersLayout.itemAtPosition(row, 3).widget().text()
#
#            answers += "\t~%s%s\n" % (answerPercent, answer)
#
#        # komplet zformatovana otazka
#        out = "%s%s {\n%s}\n" % (questionTitle, questionText, answers)
#
#        return out
#
#
#    def  fill(self, title, name, answers):
#        """ vyplnovani widgetu daty z loadnutych otazek """
#
#        # zadani otazky
#        self.fillTitle(title, name)
#        for answer in answers:
#            percent = re.search(self.percentRegexp, answer.decode("utf-8"))
#            if percent:
#                percentValue = self.str2num(percent.group(1))
#                self.addAnswer(text=percent.group(2), percentValue=percentValue, checked=True)
#            else:
#                self.addAnswer(text=answer[1:])
#
#            # no a nakonec zkontrolujeme procentulani soucet
#            # TODO !!! kdyz zjistitime ze soucet je vetsi nez 100%, tak otazku nacist, ale vypsat error okynko
#            self.validatePercent()
